package com.pcxm.cm.dao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.pcxm.cm.pojo.MaxPeriod;

@Repository
public class MaxPeriodHome {
	private static final Log log = LogFactory.getLog(MaxPeriodHome.class);

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * 根据名称查询记录
	 * 
	 * @param name
	 * @return
	 */
	public MaxPeriod getObj(String name, String moduleId) {
		String sql = "from MaxPeriod where name = ? and moduleId = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql);
		query.setParameter(0, name);
		query.setParameter(1, moduleId);
		List<MaxPeriod> result = query.list();
		if (result != null && result.size() > 0) {
			return result.get(0);
		} else {
			return null;
		}
	}

	/**
	 * @param name
	 * @param moduleId
	 * @return 最大period
	 */
	public Long getMaxPeriod(String name, String moduleId) {
		String sql = "from MaxPeriod where name = ? and moduleId = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql);
		query.setParameter(0, name);
		query.setParameter(1, moduleId);
		List<MaxPeriod> result = query.list();
		if (result != null && result.size() > 0) {
			Long period = result.get(0).getPeriod();
			return period;
		} else {
			return (long) 0;
		}
	}


	public String getId() {
		String sql = "select hibernate_sequence.nextval from dual";
		Query q = sessionFactory.getCurrentSession().createSQLQuery(sql);
		String id = q.list().get(0).toString();
		return id;
	}

	public void saveOrUpdate(MaxPeriod maxPeriod_query) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(maxPeriod_query);
	}
	
	public void update(MaxPeriod maxPeriod_query) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().update(maxPeriod_query);
	}

	public void save(MaxPeriod maxPeriod_query) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(maxPeriod_query);
	}
}
